package com.lucky.auth_boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lucky.auth_boot.pojo.dto.DomainDTO;
import com.lucky.auth_boot.pojo.dto.DomainIsShowDTO;
import com.lucky.auth_boot.pojo.dto.UserDTO;
import com.lucky.auth_boot.pojo.po.DomainPO;
import com.lucky.auth_boot.exception.BusinessException;
import com.lucky.auth_boot.mapper.DomainMapper;
import com.lucky.auth_boot.pojo.vo.DomainUserVO;
import com.lucky.auth_boot.service.DomainService;
import com.lucky.auth_boot.pojo.vo.DomainCheckInfo;
import jakarta.annotation.Resource;
import org.springframework.data.domain.Pageable;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Base64;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class DomainServiceImpl implements DomainService {

    @Resource
    private DomainMapper domainMapper;

    @Override
    public List<DomainPO> queryDomainList() {
        return domainMapper.selectList(null);
    }

    @Override
    public DomainCheckInfo queryDomainByName(String domainName) {
//        处理不是4的倍数报错问题
        if (domainName.length() % 4 != 0) {
            int paddingNeeded = 4 - domainName.length() % 4;
            // 使用paddingNeeded个等号来补全Base64字符串
            domainName += String.join("", Collections.nCopies(paddingNeeded, "="));
        }
        byte[] bytes;
        try {
            bytes = Base64.getDecoder().decode(domainName);
        } catch (Exception e) {
            throw new BusinessException("非法参数！");
        }
        String url = new String(bytes);
        QueryWrapper<DomainPO> wrapper = new QueryWrapper<>();
        wrapper.eq("auth_domain_name", url);
        DomainCheckInfo domainCheckInfo = new DomainCheckInfo();
        domainCheckInfo.setDomainName(url);
        if (domainMapper.selectCount(wrapper) > 0) {
            domainCheckInfo.setDomainStatus(1);
        } else {
            domainCheckInfo.setDomainStatus(0);
        }
        return domainCheckInfo;
    }

    @Override
    public DomainPO queryDomainById(Integer id) {
        QueryWrapper<DomainPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        return domainMapper.selectOne(queryWrapper);
    }

    @Override
    public Integer deleteDomainById(Integer id) {
        try {
            QueryWrapper<DomainPO> wrapper = new QueryWrapper<>();
            wrapper.eq("id", id);
            return domainMapper.delete(wrapper);
        } catch (Exception e) {
            throw new BusinessException(500,"操作失败");
        }
    }

    @Override
    public Integer deleteDomainByIds(Integer id) {
        try {
//            UserPO userPO = ThreadLocalUtil.get();
            UserDTO userDTO = (UserDTO) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

            DomainPO domainPO = new DomainPO();
            domainPO.setUpdater(userDTO.getId());
            domainPO.setUpdateTime(new Date());
            domainPO.setIsDelete(1);

            QueryWrapper<DomainPO> wrapper = new QueryWrapper<>();
            wrapper.eq("id", id);

            return domainMapper.update(domainPO, wrapper);
        } catch (Exception e) {
            throw new BusinessException("操作失败");
        }
    }

    @Override
    public Integer updateDomainInfoById(DomainPO domainPO) {
        try {
            UserDTO userDTO = (UserDTO) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

            domainPO.setUpdater(userDTO.getId());
            domainPO.setUpdateTime(new Date());
            domainPO.setCreator(null);
            domainPO.setCreateTime(null);

            QueryWrapper<DomainPO> wrapper = new QueryWrapper<>();
            wrapper.eq("id", domainPO.getId());

            return domainMapper.update(domainPO, wrapper);
        } catch (Exception e) {
            throw new BusinessException("操作失败");
        }
    }

    @Override
    public Integer addDomain(DomainPO domainPO) {
        UserDTO userDTO = (UserDTO) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        domainPO.setId(null);
        domainPO.setCreator(userDTO.getId());
        domainPO.setCreateTime(new Date());
        domainPO.setIsShow(1);
        domainPO.setIsDelete(0);

        QueryWrapper<DomainPO> wrapper = new QueryWrapper<>();
        wrapper.eq("auth_domain_name", domainPO.getAuthDomainName());
        if (domainMapper.selectCount(wrapper) > 0) {
            throw new BusinessException("该域名已授权");
        }
        try {
            return domainMapper.insert(domainPO);
        } catch (Exception e) {
            throw new BusinessException("操作失败");
        }
    }

    public Page<DomainUserVO> queryDomainListPage(Pageable pageable, DomainDTO domainDTO){
        Page<DomainUserVO> page = new Page<>(pageable.getPageNumber(), pageable.getPageSize());
        return domainMapper.queryDomainListPage(page,domainDTO);
    }

    @Override
    public DomainUserVO queryDomainOneById(Integer id) {
        return domainMapper.queryDomainOneById(id);
    }

    @Override
    public Integer putEmpowerUrlIsShow(DomainIsShowDTO domainIsShowDTO) {
        UserDTO userDTO = (UserDTO) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        DomainPO domainPO = new DomainPO();
        domainPO.setIsShow(domainIsShowDTO.getIsShow());
        domainPO.setUpdater(userDTO.getId());
        domainPO.setUpdateTime(new Date());

        QueryWrapper<DomainPO> wrapper = new QueryWrapper<>();
        wrapper.eq("id", domainIsShowDTO.getId());
        try {
            return domainMapper.update(domainPO, wrapper);
        } catch (Exception e) {
            throw new BusinessException("操作失败");
        }
    }

}
